package com.example.test.activity;

import com.example.test.R;
import com.example.test.R.id;
import com.example.test.R.layout;
import com.example.test.model.Constants;
import com.example.test.model.FollowBoolean;
import com.example.test.model.SameFollowProcess;






import com.example.test.model.social_api.NetAPI;
import com.example.test.model.social_api.QQAPI;
import com.example.test.model.social_api.SinaAPI;
import com.netease.microblog.oauth2.Oauth2;
import com.tencent.weibo.sdk.android.model.GlobalVar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class FindFriActivity extends Activity {

	private static final float TEXTSIZE=17.0f;
	private Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constants.MSG_GETFOLLOWLIST_SUCCESS:
				setFollowList((FollowBoolean[])msg.obj);
				break;
			case Constants.MSG_FOLLOW_SUCCESS:
				SameFollowProcess.giveSameResult(FindFriActivity.this,handler);
				Toast.makeText(FindFriActivity.this, "成功关注", 1000).show();
				break;
			default:
				break;
			}
			
			super.handleMessage(msg);
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_findfri);
		SameFollowProcess.giveSameResult(this,handler);
	}
	
	void setFollowList(FollowBoolean [] result)
	{
		ProgressBar bar=(ProgressBar)findViewById(R.id.waitbar);
		bar.setVisibility(View.INVISIBLE);
		if(result==null)
			return;
		LinearLayout.LayoutParams params2=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		params2.gravity=Gravity.CENTER;
		TableLayout layout=(TableLayout)findViewById(R.id.tlFoloowList);
		layout.removeAllViews();
		TableRow firstRow=new TableRow(this);
		LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		firstRow.setLayoutParams(params);
		TextView fview=new TextView(this);
		fview.setText("用户名");
		fview.setBackgroundResource(R.drawable.tablestyle);
		fview.setGravity(Gravity.CENTER);
		fview.setTextSize(TEXTSIZE);
		firstRow.addView(fview);
		TextView fsinaView=new TextView(this);
		fsinaView.setText("新浪微博");
		fsinaView.setBackgroundResource(R.drawable.tablestyle);
		fsinaView.setTextSize(TEXTSIZE);
		fsinaView.setGravity(Gravity.CENTER);;
		firstRow.addView(fsinaView);
		TextView fqqView=new TextView(this);
		fqqView.setText("腾讯微博");
		fqqView.setBackgroundResource(R.drawable.tablestyle);;
		fqqView.setTextSize(TEXTSIZE);
		fqqView.setGravity(Gravity.CENTER);;
		firstRow.addView(fqqView);
		TextView fnetView=new TextView(this);
		fnetView.setText("网易微博");
		fnetView.setBackgroundResource(R.drawable.tablestyle);;
		fnetView.setGravity(Gravity.CENTER);
		fnetView.setTextSize(TEXTSIZE);
		firstRow.addView(fnetView);
		layout.addView(firstRow);
		for (final FollowBoolean followBoolean : result) {
			if(followBoolean.isSina()||followBoolean.isQq()||followBoolean.isNet())
			{
				if(!(followBoolean.isSina()&&followBoolean.isQq()&&followBoolean.isNet()))
				{
					TableRow row=new TableRow(this);
					LayoutParams params1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					row.setLayoutParams(params1);
					TextView view=new TextView(this);
					view.setBackgroundResource(R.drawable.tablestyle);;
					view.setText(followBoolean.getUidInfo().getUserName());
					view.setTextSize(TEXTSIZE);
					view.setGravity(Gravity.CENTER);
					row.addView(view);
					TextView sinaView=new TextView(this);
					sinaView.setBackgroundResource(R.drawable.tablestyle);
					sinaView.setText(followBoolean.isSina()?"已关注":followBoolean.getUidInfo().getSina()!=null?"未关注":"未注册");
					sinaView.setGravity(Gravity.CENTER);
					sinaView.setTextSize(TEXTSIZE);
					sinaView.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View arg0) {
							if(GlobalVar.sinaUid==null||GlobalVar.sinaUid.trim().equals(""))
							{
								Toast.makeText(FindFriActivity.this, "你尚未绑定新浪微博！", 1000).show();	
							    return;
							}
							
							String status=((TextView)arg0).getText().toString();
							if(status.equals("已关注"))
                             Toast.makeText(FindFriActivity.this, "你已关注其新浪微博！", 1000).show();
							else if(status.equals("未注册"))
							 Toast.makeText(FindFriActivity.this, "对方尚未注册新浪微博！", 1000).show();
							else new AlertDialog.Builder(FindFriActivity.this).setMessage("你确定要关注该用户的新浪微博么？").
							                     setPositiveButton("确定", new DialogInterface.OnClickListener() {
													
													@Override
													public void onClick(DialogInterface arg0, int arg1) {
														SinaAPI.follow(handler, followBoolean.getUidInfo().getSina());
														
														
													}
												}).setNegativeButton("取消", null).create().show();
							
						}
					});
					row.addView(sinaView);
					TextView qqView=new TextView(this);
					qqView.setText(followBoolean.isQq()?"已关注":followBoolean.getUidInfo().getQq()!=null?"未关注":"未注册");
					qqView.setGravity(Gravity.CENTER);
					qqView.setTextSize(TEXTSIZE);
					qqView.setBackgroundResource(R.drawable.tablestyle);
                    qqView.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View arg0) {
							if(GlobalVar.QQToken==null||GlobalVar.QQToken.trim().equals(""))
							{
								Toast.makeText(FindFriActivity.this, "你尚未绑定腾讯微博！", 1000).show();	
							    return;
							}
							
							String status=((TextView)arg0).getText().toString();
							if(status.equals("已关注"))
                             Toast.makeText(FindFriActivity.this, "你已关注其腾讯微博！", 1000).show();
							else if(status.equals("未注册"))
							 Toast.makeText(FindFriActivity.this, "对方尚未注册腾讯浪微博！", 1000).show();
							else new AlertDialog.Builder(FindFriActivity.this).setMessage("你确定要关注该用户的腾讯微博么？").
							                     setPositiveButton("确定", new DialogInterface.OnClickListener() {
													
													@Override
													public void onClick(DialogInterface arg0, int arg1) {
														QQAPI.follow(FindFriActivity.this, followBoolean.getUidInfo().getQq(),handler);
														
													}
												}).setNegativeButton("取消", null).create().show();
							
						}
					});
					row.addView(qqView);
					TextView netView=new TextView(this);
					netView.setText(followBoolean.isNet()?"已关注":followBoolean.getUidInfo().getNet()!=null?"未关注":"未注册");
					netView.setGravity(Gravity.CENTER);
					netView.setTextSize(TEXTSIZE);
					netView.setBackgroundResource(R.drawable.tablestyle);
                    netView.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View arg0) {
							if(Oauth2.getInstance().getAccessToken()==null||Oauth2.getInstance().getAccessToken().trim().equals(""))
							{
								Toast.makeText(FindFriActivity.this, "你尚未绑定网易微博！", 1000).show();	
							    return;
							}
							
							String status=((TextView)arg0).getText().toString();
							if(status.equals("已关注"))
                             Toast.makeText(FindFriActivity.this, "你已关注其网易微博！", 1000).show();
							else if(status.equals("未注册"))
							 Toast.makeText(FindFriActivity.this, "对方尚未注册网易微博！", 1000).show();
							else new AlertDialog.Builder(FindFriActivity.this).setMessage("你确定要关注该用户的网易微博么？").
							                     setPositiveButton("确定", new DialogInterface.OnClickListener() {
													
													@Override
													public void onClick(DialogInterface arg0, int arg1) {
														NetAPI.follow(followBoolean.getUidInfo().getNet(), handler);
														
													}
												}).setNegativeButton("取消", null).create().show();
							
						}
					});
					row.addView(netView);
					layout.addView(row);
				}
			}
		}
	}

}
